 <?php if (!isset($_SESSION['login']) or !isset($_SESSION['senha'])) {
 	header('location: login.html');
 }
 	  
 ?>

<script type="text/javascript">

function excluir(codigo){
	var resp = confirm("Deseja realmente excluir esse registro?");

	if(resp){
		document.location = "index.php?page=ferramenta/controleexcluir.php&codigo=" + codigo;
	}
}
</script>

<html>
<head>
	<meta http-equiv="content-type" content="text/html: charset=utf-8" />
	<script type="text/javascript" charset="utf-8">
	function paint (id) {
		
		for (var i = 0; i < id; i++) {
			if (i % 2 == 0) {
				document.getElementById(i).style.backgroundColor='#E6F9F7';
			}else{
				document.getElementById(i).style.backgroundColor='#BED6F4';
			}
		}
	}
	</script>
</head>
<body>
	<div class="flist">
		<table class="tabela">
			<tr>
				<th <?php if($_SESSION['permissao'] === "2"){ ?> colspan="7" <?php }else ?> colspan="6">Lista de Ferramentas</th>
			</tr>
		    <tr>
				<th>Nº Ferramenta</th>
				<th>Desc. Ferramenta</th>
				<th>Marca</th>
				<th>Categoria</th>
				<th>Situação</th>
				<th>Data Aquisição</th>
				<?php if($_SESSION['permissao'] === "2"){ ?><th>Ação</th><?php } ?>
			</tr>
		   	<?php $i = 0;?>
			<?php 
				include "conexao/conexao.php";
			
			$sql = "SELECT * FROM ferramenta"; 
			$result = mysql_query($sql) or die("Erro ao selecionar a tabela de ferramentas: ".mysql_error());
									
			while($linha = mysql_fetch_array($result)):	
								//var_dump($linha);
			?>					
			
			<tr id="<?php echo $i; ?>">
				<td><?php echo $linha['nu_ferramenta']?></td>
				<td><?php echo $linha['descricao']?></td>
				<td><?php echo $linha['no_fabricante']?></td>
				<td><?php
					 $sqlcate = "SELECT * FROM categoria_ferramenta";
					 $resultCate = mysql_query($sqlcate);
					 $arResultCate = mysql_fetch_assoc($resultCate);
					 do{
					 	if($arResultCate['idcategoria'] == $linha['idcategoria']){
					 		echo $arResultCate['nome_categoria'];
					 	}
					 }while($arResultCate = mysql_fetch_assoc($resultCate))?></td>
				<td><?php 
					$sqlSit = "SELECT * FROM situacao";
					$reSit = mysql_query($sqlSit);
					$arSit = mysql_fetch_assoc($reSit);
					do{
						if($arSit['idsituacao'] == $linha['idsituacao']){
							echo $arSit['descricao'];
						}
					}while($arSit = mysql_fetch_assoc($reSit))
					?></td>
				<td>
					<?php 
					$data = $linha['dt_aquisicao'];
					$dataformatada = new DateTime($data);
					$date = $dataformatada->format('d/m/Y');
					$time = $dataformatada->format('H:i:s');
					echo $date." &nbsp".$time;
					?>
				</td>
				<?php if($_SESSION['permissao'] === "2"){ ?>
				<td>
					
			 		<a href="index.php?page=ferramenta/editar.php&id=<?php echo $linha['nu_ferramenta']?>">
			 			<img src="imagens/pen2.png" title="Editar" height="32" width="32"></a> &nbsp
					
					<a href="javascript:void(0)" onclick="excluir(<?php echo $linha['nu_ferramenta'];?>)">
						<img src="imagens/exc.png" title="Excluir" height="32" width="32"></a>
				</td>
				<?php } ?>
			</tr>
			<?php $i++; ?>
			<?php endwhile;?>
		</table>
			<script type="text/javascript">
				var i = '<?php echo $i; ?>'; 
				paint(i);
			</script>
	</div>
</body>
</html>
